package com.chif.goingplus.controller;


import com.chif.goingplus.pojo.Emp;
import com.chif.goingplus.service.AdminServiceImpl;
import com.chif.goingplus.service.EmpService;
import com.chif.goingplus.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private AdminServiceImpl adminService;

    @Autowired
    private EmpService empService;

    @RequestMapping("/login")
    public String checkLogin(String username, String password, HttpSession session){
        int resultNum = adminService.checkLogin(username, password);
        Map<String,Object> resultMap=new HashMap<String, Object>();
        if (resultNum==0){
            resultMap.put("error","用户名不存在");
        }
        if (resultNum==1){
            resultMap.put("error","密码不正确");
        }
        if (resultNum==2){
            Emp userInfo = empService.queryEmpByUserName(username);
            resultMap.put("userInfo",userInfo);
            session.setAttribute("userInfo",userInfo);
            //System.out.println(userInfo.getEname()+"登入了");
        }
        return JsonUtils.getJson(resultMap);
    }

    /**
     * 从Session中获取用户数据
     * @return
     */
    @RequestMapping("/getUserInfo")
    public String getUserInfoFromSession(HttpSession httpSession){

        Emp userInfo=null;
        try{
             userInfo= (Emp) httpSession.getAttribute("userInfo");
        }catch (Exception e){
            System.out.println("未登录");
        }

        return JsonUtils.getJson(userInfo);
    }

    /**
     * 注销登录
     * 从Session中销毁用户数据,并返回首页
     */
    @RequestMapping("/delUserInfo")
    public String delUserInfoFromSession(HttpSession httpSession){
        httpSession.removeAttribute("userInfo");
        httpSession.invalidate();
        return JsonUtils.getJson("OK");
    }

//    @RequestMapping("/user/register")
//    @ResponseBody
//    public String registerUser(String userName,String nickName,String passWord,String email,HttpSession session){
//        Map<String,String> resultMap=new HashMap<String, String>();
//        if (userName==""){
//            //用户名不能为空！
//            resultMap.put("error","用户名不能为空！");return JsonUtils.getJson(resultMap);
//        }if (passWord==""){
//            //密码不能为空！
//            resultMap.put("error","密码不能为空！");return JsonUtils.getJson(resultMap);
//        }if (nickName==""){
//            //昵称不能为空！
//            resultMap.put("error","昵称不能为空！");return JsonUtils.getJson(resultMap);
//        }if (email==""){
//            //昵称不能为空！
//            resultMap.put("error","绑定邮箱不能为空！");return JsonUtils.getJson(resultMap);
//        }
//        Blogger blogger = new Blogger();
//        blogger.setUserName(userName);
//        blogger.setNickName(nickName);
//        blogger.setPassword(passWord);
//        blogger.setEmail(email);
//        blogger.setImageName("img\\about-img\\imageName_miss.jpg");
//        blogger.setSign("这个人很懒，什么都没有留下");
//        //注册
//        int id = bloggerService.registerBlogger(blogger);
//        if (id==0){
//            //用户名已经存在！
//            resultMap.put("error","用户名太热门了，请换个用户名试试！");return JsonUtils.getJson(resultMap);
//        }
//        Blogger userInfo = bloggerService.getBloggerById(id);
//        resultMap.put("userName",userInfo.getUserName());
//        session.setAttribute("userInfo",userInfo);
//        return JsonUtils.getJson(resultMap);
//    }
}
